package logic

import (
	"context"
	"weee/common"
	"weee/userrpc/userrpcclient"

	"weee/api/internal/svc"
	"weee/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSendLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendLoginLogic {
	return &SendLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SendLoginLogic) SendLogin(req *types.SendCodeRequest) (resp *types.SendCodeResponse, err error) {
	// todo: add your logic here and delete this line
	code, err := common.SendCode(req.Mobile)
	if err != nil {
		return &types.SendCodeResponse{
			Code: 400,
			Msg:  "验证码发送失败",
		}, nil
	}
	res, _ := l.svcCtx.UserRpc.SendCode(l.ctx, &userrpcclient.SendCodeRequest{
		Mobile: req.Mobile,
		Code:   code,
	})
	if res.Code != 200 {
		return &types.SendCodeResponse{
			Code: int(res.Code),
			Msg:  res.Msg,
		}, nil
	}
	return &types.SendCodeResponse{
		Code: int(res.Code),
		Msg:  res.Msg,
		Data: code,
	}, nil
}
